//
//  LinkageViewController.m
//  YYPopMenuView
//
//  Created by 蓓蕾 on 2021/8/28.
//

#import "LinkageViewController.h"

@interface LinkageViewController ()<UITableViewDelegate, UITableViewDataSource, YYMenuDataSource, YYMenuDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) UIView *sectionView;

@property (nonatomic, strong) UILabel *sectionTwoView;

@property (nonatomic, strong) YYTopMenuView *menuView;
@property (nonatomic, strong) YYTopMenuView *sectionMenuView;

@property (nonatomic, strong) NSArray *sortArr;
@property (nonatomic, strong) NSArray *levelArr;
@property (nonatomic, strong) NSArray *areaArr;

@end

@implementation LinkageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    
    _sortArr = @[@"综合排序", @"预约最高", @"低价优先", @"高价优先", @"距离最近"];
    _levelArr = @[@"不限", @"公立三甲", @"公立医院", @"民营医院", @"专业机构"];
    _areaArr = @[@"不限", @"郑东新区", @"航空港区", @"中原区", @"二七区", @"管城回族区", @"金水区", @"上街区", @"惠济区", @"高新区"];
    
    [self initTableView];
}

-(void)initTableView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, YYMenu_SCREEN_WIDTH, YYMenu_SCREEN_HEIGHT) style:UITableViewStyleGrouped];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
//    _tableView.backgroundColor = rgbColor(0xeeeeee);
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 0;
    }
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 390;
    }
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
//        self.sectionView.backgroundColor = rgbColor(0xeeeeee);
        return self.sectionView;
    }
//    self.sectionTwoView.backgroundColor = rgbColor(0xffffff);
//    self.sectionTwoView.backgroundColor = [UIColor orangeColor];
    
    [self.tableView addSubview:self.sectionMenuView];
    return self.sectionTwoView;
}

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(nonnull UIView *)view forSection:(NSInteger)section
{
    if (section == 0) {
        NSLog(@"隐藏悬浮视图");
        self.menuView.hidden = YES;
    }
    
}

- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section
{
    if (section == 0) {
        NSLog(@"展示悬浮视图");
        self.menuView.hidden = NO;
    }
}
// 标记一下RightTableView的滚动方向，是向上还是向下
//- (void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
//    static CGFloat lastOffsetY = 0;
//
//    _isScrollDown = lastOffsetY < scrollView.contentOffset.y;
//    lastOffsetY = scrollView.contentOffset.y;
//}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

-(UIView *)sectionView
{
    if (!_sectionView) {
        _sectionView = [[UIView alloc] init];
        _sectionView.backgroundColor = rgbColor(0xffffff);
    }
    return _sectionView;
}
//有多少个column，默认为1列
- (NSInteger)numberOfColumnsInMenu:(YYTopMenuView *)menu
{
    return 4;
}

//每个column有多少行
- (NSInteger)menu:(YYTopMenuView *)menu numberOfRowsInColumn:(NSInteger)column;
{
    switch (column) {
        case 0:
            return _sortArr.count;
            break;
        case 1:
            return _levelArr.count;
            break;
        case 2:
            return _areaArr.count;
            break;
        case 3:
            return 0;
            break;
            
        default:
            break;
    }
    return 0;
}
//每个column中每行的title
- (NSString *)menu:(YYTopMenuView *)menu titleForRowAtIndexPath:(YYIndexPath *)indexPath;
{
    switch (indexPath.column) {
        case 0:
//            return @"综合排序";
            return _sortArr[indexPath.row];
            break;
        case 1:
//            return @"医院等级";
            return _levelArr[indexPath.row];
            break;
        case 2:
//            return @"区域";
            return _areaArr[indexPath.row];
            break;
        case 3:
            return @"筛选";
            break;
            
        default:
            break;
    }
    return @"";
}

-(UILabel *)sectionTwoView
{
    if (!_sectionTwoView) {
        _sectionTwoView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, YYMenu_SCREEN_WIDTH, 50)];
        _sectionTwoView.textAlignment = NSTextAlignmentCenter;
        _sectionTwoView.text = @"第二部分";
        
//        [self createDropMenu];
//        [_sectionTwoView addSubview:self.dropMenu];
    }
    return _sectionTwoView;
}

-(YYTopMenuView *)menuView
{
    if (!_menuView) {
        _menuView = [[YYTopMenuView alloc] initWithOrigin:CGPointMake(0, 0) andDefaultColumnArr:@[@"综合排序",@"医院等级",@"区域",@"筛选"] andNormalImageArr:@[] andSelectImageArr:@[] andHeight:50];
        _menuView.leftTitleArr = @[@"体检类型",@"预约时间"];
        _menuView.rightContentArr = @[@[@"不限",@"核酸检测",@"入职体检",@"全身体检",@"青年体检",@"中年体检",@"老年体检",@"肿瘤筛选",@"孕前检查",@"婚前检查",@"疾病筛选",@"儿童体检",@"健康证体检",@"特惠套餐"],@[@"不限",@"周六可约",@"周天可约",@"提前1天约",@"提前2天约",@"提前3天约",@"3天以上"]];
        [self.view addSubview:_menuView];
        _menuView.hidden = YES;
        _menuView.showAnimation = NO;
//        _sectionMenuView.fatherView = self.view;
        _menuView.dataSource = self;
        _menuView.delegate = self;
        [_menuView selectDeafultIndexPath];
        [_menuView initSelectView];
        YYMenuWeakSelf
        _menuView.ShaiXuanBlock = ^{
            if (!weakSelf.sectionMenuView.shaiXuanViewHidden) {
                [weakSelf.sectionMenuView hiddenSelectView];
                [weakSelf.menuView hiddenSelectView];
            }
        };
        _menuView.SelectIndexPathBlock = ^(YYIndexPath * _Nonnull indexPath) {
            NSLog(@"当前选中的是第 %ld 项，第 %ld 行",indexPath.column,indexPath.row);
            [weakSelf.sectionMenuView selectItemWithIndexPath:indexPath];
        };
        
        _menuView.SelectShaiXuanItemBlock = ^(NSInteger leftRow, NSInteger index) {
            NSLog(@"选中的是 %ld 部分的第 %ld 个",leftRow,index);
            [weakSelf.sectionMenuView selectShaiXuanItemWithLeftRow:leftRow withIndex:index];
        };
    }
    return _menuView;
}

-(YYTopMenuView *)sectionMenuView
{
    if (!_sectionMenuView) {
        _sectionMenuView = [[YYTopMenuView alloc] initWithOrigin:CGPointMake(0, 390) andDefaultColumnArr:@[@"综合排序",@"医院等级",@"区域",@"筛选"] andNormalImageArr:@[] andSelectImageArr:@[] andHeight:50];
        _sectionMenuView.leftTitleArr = @[@"体检类型",@"预约时间"];
        _sectionMenuView.rightContentArr = @[@[@"不限",@"核酸检测",@"入职体检",@"全身体检",@"青年体检",@"中年体检",@"老年体检",@"肿瘤筛选",@"孕前检查",@"婚前检查",@"疾病筛选",@"儿童体检",@"健康证体检",@"特惠套餐"],@[@"不限",@"周六可约",@"周天可约",@"提前1天约",@"提前2天约",@"提前3天约",@"3天以上"]];
        [self.tableView addSubview:_sectionMenuView];
        _sectionMenuView.showAnimation = NO;
//        _sectionMenuView.fatherView = self.view;
        _sectionMenuView.shaiXuanViewHidden = YES;
        _sectionMenuView.dataSource = self;
        _sectionMenuView.delegate = self;
        [_sectionMenuView selectDeafultIndexPath];
        [_sectionMenuView initSelectView];
        YYMenuWeakSelf
        _sectionMenuView.SelectIndexPathBlock = ^(YYIndexPath * _Nonnull indexPath) {
            NSLog(@"当前选中的是第 %ld 项，第 %ld 行",indexPath.column,indexPath.row);
            [weakSelf.menuView selectItemWithIndexPath:indexPath];
        };
        
        _sectionMenuView.SelectShaiXuanItemBlock = ^(NSInteger leftRow, NSInteger index) {
            NSLog(@"选中的是 %ld 部分的第 %ld 个",leftRow,index);
            [weakSelf.menuView selectShaiXuanItemWithLeftRow:leftRow withIndex:index];
        };
        _sectionMenuView.GetCurrenSelectColumnBlock = ^(NSInteger column, BOOL isShow) {
            if (!weakSelf.menuView.hidden) {
                NSLog(@"当前选中的是 第 %ld 项",column);
                [weakSelf.menuView showColumnView:column];
            }
            
        };
        
        _sectionMenuView.SelectShaiXuanBlock = ^{
            if (!weakSelf.menuView.hidden) {
                NSLog(@"点击了筛选");
                [weakSelf.menuView ShaiXuanMethod];
            }
            
        };
        
        _sectionMenuView.HiddenShaiXuanViewBlock = ^{
            [weakSelf.menuView hiddenSelectView];
        };
    }
    return _sectionMenuView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
